package controller

import (
	"github.com/gin-gonic/gin"
	"github.com/gorilla/sessions"
	"project/study-go/ecode"
	"project/study-go/http/middleware"
	"project/study-go/logic"
)

type UserController struct {}

func (u UserController) RegisterRoute(g *gin.RouterGroup) {
	user := g.Group("/user")
	{
		user.GET("",middleware.SessionAuth(),GetUserSelfInfo)
		user.GET("/:username",middleware.SessionAuth(),GetUserInfo)
	}
}

//GetUserSelfInfo 得到用户自身信息
func GetUserSelfInfo(c *gin.Context){
	//获取session
	session, ok := c.Get("SESSIONID")
	if !ok {
		c.JSON(403, ecode.Response{
			Code: 403,
			Msg:  "StatusForbidden",
			Data: nil,
		})
		return
	}

	uid := session.(*sessions.Session).Values["uid"]
	user, errCode := logic.DefaultUser.GetUserInfoByUid(uid.(string))
	if errCode.Err != nil{
		c.JSON(errCode.Code,ecode.Response{
			Code: errCode.Code,
			Msg:  errCode.Err,
			Data: nil,
		})
		return
	}

	c.JSON(200, ecode.Response{
		Code: 200,
		Msg:  "ok",
		Data: user,
	})
}

//GetUserInfo 获取用户信息
func GetUserInfo(c *gin.Context){

}